<script setup lang="ts">
import { RouterLink } from 'vue-router'
import SubmitBtn from '@/components/button/SubmitBtn.vue'
import { useUserStore } from '@/stores'
import { removeToken } from '@/utils/auth'
import { logoutAPI } from '@/apis/user'
import { useRouter } from 'vue-router'
import { storeToRefs } from 'pinia'

const userStore = useUserStore()
const router = useRouter()
const { userInfo } = storeToRefs(userStore)

function handleLogout() {
  // 清理
  removeToken()
  logoutAPI()
  userStore.clearUserInfo()
  // 跳转
  router.push('/login')
}
</script>

<template>
  <div class="top-banner">
    <RouterLink to="/questions">
      <div class="banner-left">
        <!-- <div class="logo">logo</div> -->
        <div class="site-title">{{ $t('banner.embossLeft') }}</div>
      </div>
    </RouterLink>
    <div v-if="!userInfo" class="banner-right">
      <RouterLink to="/login">
        <div class="btn"> 登录 </div>
      </RouterLink>
      <RouterLink to="/register">
        <div class="btn">注册</div>
      </RouterLink>
    </div>
    <div v-else class="banner-right">
      <RouterLink to="/questions/ask">
        <SubmitBtn>提问</SubmitBtn>
      </RouterLink>
      <RouterLink to="/users/notifications/inbox">
        <div>通知</div>
      </RouterLink>
      <RouterLink to="/users/notifications/achievement">
        <div>成就</div>
      </RouterLink>

      <div class="avatar-container">
        <div v-if="!userInfo?.user.avatar" class="avatar">
          {{ userInfo?.user.displayName[0].toUpperCase() }}
        </div>
        <img v-else class="avatar" :src="userInfo?.user.avatar" alt="avatar" />
        <ul class="dropdown-container">
          <RouterLink :to="`/users/${userInfo?.user.username}`">
            <li class="dropdown-item">用户主页</li>
          </RouterLink>
          <RouterLink to="/users/settings/profile">
            <li class="dropdown-item">账号设置</li>
          </RouterLink>
          <li class="dropdown-item" @click="handleLogout">退出</li>
        </ul>
      </div>

    </div>
  </div>
</template>

<style scoped lang="scss">
.top-banner {
  position: relative;
  z-index: 600;
  border-bottom: 2px solid var(--site-banner-border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 1.5em;
  background: var(--site-banner-bgc);
  min-height: 60px;
}

.site-title {
  text-align: center;
  align-self: center;
  font-family: "Consolas", serif;
  text-transform: uppercase;
  margin-right: .25em;
  font-size: 4.500rem;
  white-space: nowrap;
  color: var(--site-banner-emboss-r);
}

.banner-left {
  display: flex;
  align-items: center;
  gap: 1em;
}

.banner-right {
  display: flex;
  justify-content: space-around;
  align-items: center;
  gap: 20px;

  .btn {
    padding: 0.5em 1em;
    border-radius: 0.25em;
    background-color: #e8eee3;
    color: #408400;
    font-size: 1.25em;
    cursor: pointer;

    &:hover {
      background-color: #b2c1a4;
      color: #00220a;
    }
  }

  .avatar {
    width: 40px;
    height: 40px;
    border-radius: 10%;
    object-fit: cover;
    border: 1px solid var(--border-primary);
    color: rgb(0, 149, 99);
    background-color: #e2efd4;
    line-height: 40px;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    cursor: pointer;
  }
}

.avatar-container {

  position: relative;

  &:hover .dropdown-container {
    visibility: visible;
    opacity: 1;
  }
}

.dropdown-container {
  position: absolute;
  visibility: hidden;
  margin-top: 0px;
  right: 0;
  opacity: 0;
  background-color: #dcf2e4;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 400;
  transition: all .1s ease-in;
}

.dropdown-item {
  transition: all .1s ease-out;
  padding: 5px 10px;
  word-break: keep-all;
  cursor: pointer;

  &:hover {
    background-color: #84c99b;
  }
}
</style>
